<template>
    <el-dialog ref="dialogform" :title="addEditName + title" :visible.sync="dialogVisible" width="70%"
        :before-close="handleClose">
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="120px" class="demo-ruleForm">
            <el-form-item label="栏目名称" prop="navigationName">
                <el-input v-model="ruleForm.navigationName" class="w100p" clearable placeholder="请输入"></el-input>
            </el-form-item>
            <el-form-item label="展示在导航栏" prop="displayed">
                <el-select v-model="ruleForm.displayed" class="w100p" clearable filterable placeholder="请选择">
                    <el-option label="展示" :value="true"></el-option>
                    <el-option label="不展示" :value="false"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="跳转类型" prop="functionType">
                <el-select v-model="ruleForm.functionType" class="w100p" clearable filterable placeholder="请选择">
                    <el-option v-for="item, index in enumjumpList" :key="index" :label="item.text"
                        :value="item.value"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="栏目级别" prop="level" v-if="!(ruleForm.functionType == 'URL')">
                <el-input type="number" v-model="ruleForm.level" class="w100p" clearable
                    placeholder="同一栏目最多设置三个分栏，可同时最多设置三个子栏目"></el-input>
            </el-form-item>
            <el-form-item label="跳转链接" prop="url" v-if="ruleForm.functionType == 'URL'">
                <el-input v-model="ruleForm.url" class="w100p" clearable placeholder="请输入"></el-input>
            </el-form-item>
            <el-form-item label="导航栏排序" prop="sort">
                <el-input type="number" v-model="ruleForm.sort" class="w100p" clearable placeholder="请输入"></el-input>
            </el-form-item>
            <el-form-item label="黑白名单" prop="rosterType">
                <el-select v-model="ruleForm.rosterType" class="w100p" clearable filterable placeholder="请选择">
                    <el-option v-for="item, index in enumList" :key="index" :label="item.text"
                        :value="item.value"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="Ip区间" prop="ipAdressVoList" v-if="ruleForm.rosterType !== 'RELEASE'">
                <div class="IpModule" v-for="item, index in ruleForm.ipAdressVoList" :key="index">
                    <IpModule @change="(ip) => getIp(ip, index, 'startIp')" :ipAddress="item.startIp + ''">
                    </IpModule>
                    <span>至</span>
                    <IpModule @change="(ip) => getIp(ip, index, 'endIp')" :ipAddress="item.endIp + ''">
                    </IpModule>
                    <div class="btnList">
                        <span v-show="ruleForm.ipAdressVoList.length == index + 1" @click="addIp(index)">添加</span>
                        <span class="del" v-show="ruleForm.ipAdressVoList.length > 1" @click="delIp(index)">删除</span>
                    </div>
                </div>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="cancel">取 消</el-button>
            <el-button type="primary" @click="submitForm">确 定</el-button>
        </span>
    </el-dialog>
</template>
<script>
import navigation from "@/api/navigation"
import IpModule from '@/components/IpModule';
export default {
    components: { IpModule },
    props: {
        addEditFlag: {
            type: String,
            default: () => {
                return 'add'
            }
        },
        data: {
            type: Object,
            default: () => {
                return {}
            }
        },
        title: {
            type: String,
            default: () => {
                return '操作表单'
            }
        },
        dialogVisible: {
            type: Boolean,
            default: () => {
                return false
            }
        },
        articleList: {
            type: Array,
            default: () => {
                return []
            }
        },
        enumjumpList: {
            type: Array,
            default: () => {
                return []
            }
        },
        enumList: {
            type: Array,
            default: () => {
                return []
            }
        },
    },
    data() {
        // 自定义验证规则，检查网址是否为有效的URL
        var validateUrl = (rule, value, callback) => {
            if (!value) {
                return callback(new Error('网址不能为空'));
            }
            // 正则表达式判断网址格式
            const reg = /(http|https):\/\/([\w.]+\/?)\S*/
            if (reg.test(value)) {
                callback();
            } else {
                callback('请输入正确的网址');
            }
        };
        return {
            addEditName: '添加',
            addEditApi: '',
            ruleForm: {},
            rules: {
                navigationName: [
                    { required: true, message: '请输入', trigger: 'change' }
                ],
                url: [
                    { required: true, message: '请输入', trigger: 'change' },
                    { validator: validateUrl, trigger: 'blur' }
                ],
                displayed: [
                    { required: true, message: '请选择', trigger: 'change' }
                ]
            }
        }
    },
    computed: {

    },
    watch: {
        dialogVisible(v) {
            v && this.initData()
        }
    },

    mounted() { },
    methods: {
        initData() {
            if (this.addEditFlag == 'add') {
                this.resetForm()
                this.$set(this.ruleForm, 'displayed', false);
                this.$set(this.ruleForm, 'rosterType', 'RELEASE');
                this.addEditApi = navigation.add
                this.addEditName = '添加'
            } else if (this.addEditFlag == 'edit') {
                this.ruleForm = { ...this.data }
                this.addEditApi = navigation.edit
                this.addEditName = '编辑'
                this.$set(this.ruleForm, 'ipAdressVoList', this.data.ipAdressVoList);
            }
            if (!this.ruleForm.ipAdressVoList || this.ruleForm.ipAdressVoList.length == 0) {
                this.$set(this.ruleForm, 'ipAdressVoList', [{ startIp: '', endIp: '' }]);
            }
        },
        getIp(ip, index, key) {
            if (!this.ruleForm.ipAdressVoList[index]) {
                this.$set(this.ruleForm.ipAdressVoList, index, { startIp: '', endIp: '' })
            }
            this.$set(this.ruleForm.ipAdressVoList[index], key, ip)
        },
        addIp(index) {
            this.$set(this.ruleForm.ipAdressVoList, index + 1, { startIp: '', endIp: '' })
        },
        delIp(index) {
            this.ruleForm.ipAdressVoList.splice(index, 1)
        },
        //提交表单
        submitForm() {
            this.$refs.ruleForm.validate((valid) => {
                if (valid) {
                    this.addEditApi({
                        ...this.ruleForm,
                        ipAdressVoList: this.ruleForm.rosterType == 'RELEASE' ? [] : this.ruleForm.ipAdressVoList
                    }).then(res => {
                        let success = res.code == 0
                        let msg = success ? '操作成功' : '操作失败'
                        this.$message[success ? 'success' : 'warning'](res.msg || msg)
                        success && this.cancel();
                    }).catch(err => {
                        console.log(err)
                    })
                } else {
                    return false;
                }
            })
        },
        //重置
        resetForm() {
            this.ruleForm = {};
            this.$refs.ruleForm && this.$refs.ruleForm.resetFields()
        },
        //取消关闭
        cancel() {
            this.resetForm()
            this.$emit("closeDialog")
        },
        handleClose(done) {
            this.$confirm('确认关闭？')
                .then(_ => { this.resetForm(); this.cancel() })
                .catch(_ => { });
        }
    }
};
</script>
<style lang="scss" scoped>
.IpModule {
    display: flex;
    justify-content: flex-start;
    flex-wrap: nowrap;
    line-height: 38px;

    .input {
        width: 200px;
    }

    .label {
        display: inline-block;
        width: 100px;
        padding: 0 5px;
        text-align: right;
        cursor: pointer;
    }

    .btnList {
        width: 200px;
        display: flex;
        justify-content: flex-start;
        padding-left: 15px;
        color: rgb(27, 143, 244);

        span {
            padding-left: 10px;
        }

        .del {
            color: red;
            padding-right: 5px;
        }
    }
}
</style>